package com.tulele.vtool.handler;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.*;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.qrcode.QRCodeUtil;
import com.tulele.vtool.service.*;
import com.tulele.vtool.utils.*;
import com.tulele.vtool.wechat.WechatAccountConfig;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商学院课程信息表(BBusinessInfo)表控制层
 *
 * @author makejava
 * @since 2020-10-22 15:35:29
 */
@RestController
@Slf4j
@RequestMapping("bBusinessInfo")
public class BBusinessInfoHandler {
    /**
     * 服务对象
     */
    @Resource
    private BBusinessInfoService bBusinessInfoService;

    /**
     * 服务对象
     */
    @Resource
    private BBusinessDetailsService bBusinessDetailsService;

    /**
     * 服务对象
     */
    @Resource
    private IUserInfoService userInfoService;

    /**
     * 服务对象
     */
    @Resource
    private BBusinessLecturerService bBusinessLecturerService;

//    @Autowired
//    private WxPayService wxPayService;

    @Autowired
    private IOrderInfoService orderInfoService;

//    @Autowired
//    private WechatAccountConfig wechatAccountConfig;
//
//    @Value("${wxShareImgPath}")
//    private String wxShareImgPath;

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private IArticleInfoService articleInfoService;

    @Autowired
    private UserGroupGzhService userGroupGzhService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BBusinessInfo selectOne(Integer id) {
        return this.bBusinessInfoService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param bBusinessInfo 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/insert", produces = "application/json;charset=UTF-8")
    public RespData insert(@RequestHeader("userId") String userId, @RequestBody BBusinessInfo bBusinessInfo) {

        RespData respData = new RespData();

        if (null == bBusinessInfo || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessInfo", this.bBusinessInfoService.insert(bBusinessInfo));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("新增成功");
        return respData;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @PostMapping(value = "/deleteById", produces = "application/json;charset=UTF-8")
    public RespData deleteById(@RequestHeader("userId") String userId, @Param("id") Integer id) {

        RespData respData = new RespData();

        if (null == id || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("boolean", this.bBusinessInfoService.deleteById(id));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("删除成功");
        return respData;
    }

    /**
     * 修改数据
     *
     * @param bBusinessInfo 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public RespData update(@RequestHeader("userId") String userId, @RequestBody BBusinessInfo bBusinessInfo) {

        RespData respData = new RespData();

        if (null == bBusinessInfo || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessInfo", this.bBusinessInfoService.update(bBusinessInfo));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("修改成功");
        return respData;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @PostMapping(value = "/queryById", produces = "application/json;charset=UTF-8")
    public RespData queryById(@RequestHeader("userId") String userId, @Param("id") Integer id) {

        RespData respData = new RespData();

        if (null == id || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessInfo", this.bBusinessInfoService.queryById(id));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @PostMapping(value = "/queryAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData queryAllByLimit(@RequestHeader("userId") String userId, @Param("offset") Integer offset, @Param("limit") Integer limit) {

        if (offset == null || offset.intValue() <= 0) {
            offset = 0;
        }

        if (limit == null || limit.intValue() <= 0) {
            limit = 10;
        }

        RespData respData = new RespData();

        if (StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessInfoList", this.bBusinessInfoService.queryAllByLimit(offset, limit));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param bBusinessInfo 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/selectAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData selectAllByLimit(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody BBusinessInfo bBusinessInfo) {
        RespData respData = new RespData();

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(springProfilesActive) || bBusinessInfo == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer pageNum = bBusinessInfo.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = bBusinessInfo.getPageSize();
        if (pageSize == null || pageSize.intValue() <= 0) {
            pageSize = 10;
        }

        // 精选课程
        if (bBusinessInfo.getType() != null && bBusinessInfo.getType() == -1) {
            bBusinessInfo.setChoiceness(1);
            bBusinessInfo.setType(null);
        }

        bBusinessInfo.setGzhCode(springProfilesActive);
        PageInfo<BBusinessInfo> pageInfo = this.bBusinessInfoService.selectAllByLimit(pageNum, pageSize, bBusinessInfo);

        if (null == pageInfo.getList() || pageInfo.getList().size() <= 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无更多");
            return respData;
        }
        List<BBusinessInfo> bBusinessInfoDto = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(bBusinessInfoDto)) {
            List<BBusinessInfo> collect = bBusinessInfoDto.stream().map(info -> {

                BBusinessInfo temporary = new BBusinessInfo();
                int numberCount = ToolUtil.randomNum(500, 5000);
                temporary.setId(info.getId());
                temporary.setCover(info.getCover());
                temporary.setTitle(info.getTitle());
                temporary.setContent(info.getContent());
                temporary.setLookCount(numberCount + ToolUtil.randomNum(50, 2000));//课程查看次数
                temporary.setNumberCount(numberCount);//课程分享次数
                temporary.setCreateTime(info.getCreateTime());

                return temporary;
            }).collect(Collectors.toList());

            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
            resultMap.put("msg", pageInfo.isIsLastPage());
            resultMap.put("collect", collect);

            respData.setData(resultMap);
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param bBusinessInfo 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/selectCourseById", produces = "application/json;charset=UTF-8")
    public RespData selectCourseById(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody BBusinessInfo bBusinessInfo) throws Exception {
        RespData respData = new RespData();
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(9);

        if (StringUtils.isBlank(userId) || bBusinessInfo == null || bBusinessInfo.getId() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        // 查询分享人基础信息
        Integer userIdInt = bBusinessInfo.getUserId();

        if (userIdInt == null) {
            // 如果没有分享人，查询自己基础信息
            userIdInt = Integer.parseInt(userId);
        }

        UserInfo userInfoSelf = this.userInfoService.getUserMainInfo(userIdInt);
        UserInfo userInfo = this.userInfoService.getUserMainInfo(Integer.parseInt(userId));
        resultMap.put("userInfoSelf", userInfoSelf);

        // 查询商学院课程
        BBusinessInfo bBusinessInfoService = this.bBusinessInfoService.queryById(bBusinessInfo.getId());

        if (null == bBusinessInfoService) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无数据");
            return respData;
        }

        if (bBusinessInfo.getUserId() != null && !Integer.valueOf(userId).equals(bBusinessInfo.getUserId()) && bBusinessInfo.getId() != null) {
            // 分享人不为空 并且 分享人不为自己
            PlatformSysInfo platformSysInfo= sysGzhService.getPlatformSysInfo(springProfilesActive);

            String headLine = bBusinessInfoService.getTitle();
            String kefuMsg = String.format(SysConstant.NEW_USER_LOOK_BUSINESS_INFO_PUSH_A_MSG.replace(SysConstant.frontDomain,platformSysInfo.getFrontDomain()), headLine);

            int vipStatus = Optional.ofNullable(userInfoService.getUserVipStatus(bBusinessInfo.getUserId())).orElse(0);
            if (vipStatus == 1) {
                kefuMsg = String.format(SysConstant.NEW_USER_LOOK_BUSINESS_INFO_PUSH_B_MSG.replace(SysConstant.frontDomain,platformSysInfo.getFrontDomain()), userInfo.getNickName(), headLine);
            }

            UserInfo userMainInfo = userInfoService.getUserMainInfo(bBusinessInfo.getUserId());

            //region （公众号做通）20210902
            if(userMainInfo!=null && !userMainInfo.getGzhCode().equals(springProfilesActive)){
                UserGroupOpenId userGroupOpenId= userGroupGzhService.getUserOpenId(springProfilesActive,bBusinessInfo.getUserId());
                if(userGroupOpenId!=null){
                    userMainInfo.setOpenId(userGroupOpenId.getOpenId());
                }
            }
            //endregion

            if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                kefuMsg =ToolUtil.handleMsgmenuidByIOS(kefuMsg);
            }

            WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(userMainInfo.getOpenId()).content(kefuMsg).build();
            try {

                PlatformSysInfo platformSysInfoConfig= sysGzhService.getPlatformSysInfo(springProfilesActive);
                WeixinServiceDL weixinService= WeixinServiceMap.getWeixinService(springProfilesActive);

                weixinService.getKefuService().sendKefuMessage(message);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }

            ArticleInfo articleInfo = new ArticleInfo();
            articleInfo.setId(bBusinessInfo.getId());// 商学院课程ID
            articleInfo.setArticleTitle("商学院课程");

            LookArticleInfo lookArticleInfo = new LookArticleInfo();
            lookArticleInfo.setUserId(Integer.valueOf(userId));
            lookArticleInfo.setOtherUserId(bBusinessInfo.getUserId());
            lookArticleInfo.setArticleId(bBusinessInfo.getId());

            //UserInfo lookUserInfo = userInfoService.getUserMainInfo(Integer.valueOf(userId));

            articleInfoService.lookOtherUserArticle(lookArticleInfo, articleInfo, userMainInfo.getNickName(), "4", springProfilesActive);
        }

        BBusinessInfo temporaryInfo = new BBusinessInfo();
        temporaryInfo.setId(bBusinessInfoService.getId());
        temporaryInfo.setCover(bBusinessInfoService.getCover());
        temporaryInfo.setTitle(bBusinessInfoService.getTitle());
        temporaryInfo.setContent(bBusinessInfoService.getContent());
        temporaryInfo.setLookCount(bBusinessInfoService.getLookCount());
        temporaryInfo.setNumberCount(bBusinessInfoService.getNumberCount());
        temporaryInfo.setCreateTime(bBusinessInfoService.getCreateTime());
        temporaryInfo.setOriginalPrice(bBusinessInfoService.getOriginalPrice());

        //讲师基础信息
        if (bBusinessInfoService.getAuthorId() != null) {
            BBusinessLecturer bBusinessLecturerDto = this.bBusinessLecturerService.queryById(bBusinessInfoService.getAuthorId());

            if (bBusinessLecturerDto != null) {
                BBusinessLecturer bBusinessLecturer = new BBusinessLecturer();
                bBusinessLecturer.setId(bBusinessLecturerDto.getId());
                bBusinessLecturer.setAuthorName(bBusinessLecturerDto.getAuthorName());
                bBusinessLecturer.setAuthorCover(bBusinessLecturerDto.getAuthorCover());
                bBusinessLecturer.setAuthorIntroduce(bBusinessLecturerDto.getAuthorIntroduce());
                resultMap.put("bBusinessLecturer", bBusinessLecturer);
            }
        }


        UserInfo userInfoSelfMine = this.userInfoService.queryUserMainInfoMine(Integer.parseInt(userId));

        Integer rangePrice = bBusinessInfoService.getRangePrice();
        Integer vipStatus = userInfoSelfMine.getVipStatus();
        Integer concernStatus = Optional.ofNullable(userInfoSelfMine.getConcernStatus()).orElse(0);
        resultMap.put("concernStatus", concernStatus);//关注状态

        /**
         *  1-全员免费 2-会员免费/非会员付费 3-全员付费/会员和非会员价格不同
         */
        Integer membersPrice = 0; //是否会员价 0-不是会员价 1-是会员价
        Integer playStatus = 0; //是否可以播放当前课程 0-不能 1-可以
        if (rangePrice == 1) {
            temporaryInfo.setNonMembersPrice(0.0d);
            playStatus = 1;
        } else if (rangePrice == 2 && vipStatus == 1) {
            temporaryInfo.setNonMembersPrice(0.0d);
            playStatus = 1;
            membersPrice = 1;

        } else if (rangePrice == 3 && vipStatus == 1) {
            temporaryInfo.setNonMembersPrice(bBusinessInfoService.getMembersPrice());

            // 查询是否购买过此课程
            Integer checkCourseStatus = orderInfoService.checkCourse(Integer.parseInt(userId), bBusinessInfo.getId());

            if (checkCourseStatus == 1) {
                playStatus = 1;
            }

            membersPrice = 1;
        } else {
            temporaryInfo.setNonMembersPrice(bBusinessInfoService.getNonMembersPrice());

            // 查询是否购买过此课程
            Integer checkCourseStatus = orderInfoService.checkCourse(Integer.parseInt(userId), bBusinessInfo.getId());

            if (checkCourseStatus == 1) {
                playStatus = 1;
            }
        }

        resultMap.put("membersPrice", membersPrice); //是否是会员价优惠价格 0-不是 1-是
        resultMap.put("playStatus", playStatus); //是否可以播放当前课程 0-不能 1-可以
        resultMap.put("temporaryInfo", temporaryInfo);

        // 查询商学院课程详情
        Integer pageNum = bBusinessInfo.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = bBusinessInfo.getPageSize();
        if (pageSize == null || pageSize.intValue() <= 0) {
            pageSize = 10;
        }

        BBusinessDetails bBusinessDetails = new BBusinessDetails();
        bBusinessDetails.setBusinessId(bBusinessInfo.getId());

        PageInfo<BBusinessDetails> pageInfo = this.bBusinessDetailsService.selectAllByLimit(pageNum, pageSize, bBusinessDetails);

        if (null == pageInfo.getList() || pageInfo.getList().size() <= 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无更多");
            return respData;
        }

        List<BBusinessDetails> queryAllByLimitDto = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(queryAllByLimitDto)) {
            Integer finalPlayStatus = playStatus; //是否可以播放当前课程 0-不能 1-可以
            List<BBusinessDetails> collect = queryAllByLimitDto.stream().map(info -> {
                BBusinessDetails temporary = new BBusinessDetails();

                String path = "";
                if (finalPlayStatus == 1) {
                    path = info.getPath();
                }
                temporary.setPath(path);
                temporary.setType(info.getType());
                temporary.setPathTime(info.getPathTime());
                temporary.setBusinessId(info.getBusinessId());
                temporary.setTitle(info.getTitle());
                temporary.setContent(info.getContent());
                temporary.setLookCount(ToolUtil.randomNum(500, 5000));

                return temporary;
            }).collect(Collectors.toList());

            resultMap.put("msg", pageInfo.isIsLastPage());
            resultMap.put("collect", collect);

        }

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param bBusinessInfo 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/creatPoster", produces = "application/json;charset=UTF-8")
    public RespData creatPoster(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody BBusinessInfo bBusinessInfo) throws Exception {

        RespData respData = new RespData();
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);

        if (StringUtils.isBlank(userId) || bBusinessInfo == null || bBusinessInfo.getId() == null || bBusinessInfo.getContent() == null || bBusinessInfo.getType() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        PlatformSysInfo platformSysInfoConfig = sysGzhService.getPlatformSysInfo(springProfilesActive);
        String wxShareImgPath = platformSysInfoConfig.getWxShareImgPath();

        // 查询分享人基础信息
        Integer userIdInt = bBusinessInfo.getUserId();

        Integer id = bBusinessInfo.getId();

        String content = bBusinessInfo.getContent();

        Integer type = bBusinessInfo.getType();

        if (userIdInt == null) {
            // 如果没有分享人，查询自己基础信息
            userIdInt = Integer.parseInt(userId);
        }
        UserInfo userInfoSelf = this.userInfoService.getUserMainInfo(userIdInt);

        // 查询商学院课程
        BBusinessInfo bBusinessInfoService = this.bBusinessInfoService.queryById(id);

        // 获取类所在项目路径，获取方式有多种
//        String outPutPath = "D:/temp/image/poster/";
//        outPutPath = URLDecoder.decode(outPutPath, "utf-8");
//wxShareImgPath
        String backgroundPath = wxShareImgPath + "poster" + type + ".png";
        String avatarPath = userInfoSelf.getAvatarUrl();
        String coverPath = bBusinessInfoService.getCover();

        // 存放在二维码中的内容
        String text = content;
        if (text == null) {
            text = "我是二维码";
        }
//        text = "http://test.qishangjinfu.com/pages/commercialCollege/courseDetails.html?id=1002&userId=1226314";
        // 嵌入二维码的图片路径
        String imgPath = "D:/temp/image/zhihu/v2-1ea1f4b00aac61996d58f7fedcc80058_hd.jpg";
        imgPath = "";
        //生成二维码
        BufferedImage qrCodePath = QRCodeUtil.encodeReturnBufferedImage(text, imgPath, false);
//        String qrCodePath = "http://img1.gtimg.com/13/1310/131008/13100832_980x1200_0.jpg";

        String makingHeadUrl = null;
        File file = null;

        // 合成图片
        file = SharedImageUtils.overlapBufferedImage(backgroundPath, avatarPath, coverPath, qrCodePath, wxShareImgPath);

        String fileName = file.getName();
        String formatDate = DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate());
        String dirName = SysConstant.TEMPORARY_FILES_OSS_DIR + userId + "/" + formatDate + "/";
        // 上传
        AliyunOssClientUtil.uploadFileByLocalFile(file, dirName + fileName);

        makingHeadUrl = SysConstant.USER_QRCODE_OSS_ACCESS_PREFIX + dirName + fileName;

        resultMap.put("makingHeadUrl", makingHeadUrl);
        respData.setData(resultMap);

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @return 实例对象
     */
    @PostMapping(value = "/queryAllCourse", produces = "application/json;charset=UTF-8")
    public RespData queryAllCourse(@RequestHeader("userId") String userId, @RequestBody BBusinessPages bBusinessPages) {

        // 查询商学院课程详情
        Integer pageNum = bBusinessPages.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = bBusinessPages.getPageSize();
        if (pageSize == null || pageSize.intValue() <= 0) {
            pageSize = 10;
        }

        RespData respData = new RespData();
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(Integer.parseInt(userId));

        PageInfo<OrderInfo> pageInfo = this.orderInfoService.selectAllCourseId(pageNum, pageSize, orderInfo);

        if (null == pageInfo) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("暂无数据");
            return respData;
        }

        List<OrderInfo> OrderInfoDto = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(OrderInfoDto)) {
            List<BBusinessInfo> collect = OrderInfoDto.stream().map(e -> {

                BBusinessInfo temporary = new BBusinessInfo();

                Integer courseId = e.getItemId();
                BBusinessInfo info = this.bBusinessInfoService.queryById(courseId);

                temporary.setId(info.getId());
                temporary.setCover(info.getCover());
                temporary.setTitle(info.getTitle());
                temporary.setContent(info.getContent());
                temporary.setLookCount(info.getLookCount());
                temporary.setNumberCount(info.getNumberCount());
                temporary.setCreateTime(info.getCreateTime());

                return temporary;
            }).collect(Collectors.toList());

            resultMap.put("msg", pageInfo.isIsLastPage());
            resultMap.put("collect", collect);
        }

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }
}